<?php if(!defined('WDK_PATH')) exit('No direct script access allowed');
require_once BASE_PATH.'admin/func.php';
abstract class Controller extends WdkController
{	
	protected $keywords = '';
	protected $description = '';
	function __construct()
	{
		parent::__construct();
		$this->config = Wdk::config();
		date_default_timezone_set($this->config['timezone']);
		
		$this->session = Wdk::helper('UserSession');
		
		$this->checkUrl();
		
		$this->tpl = new Template_();
		$this->tpl->compile_dir = CACHE_PATH.'tpl_/_compile';
		$this->tpl->template_dir = BASE_PATH;
		$this->tpl->cache_dir = CACHE_PATH.'tpl_/_cache';
		$this->uri = Wdk::helper('Uri');		
		
		$this->layout = 'default';
// 		$this->_currentAccess();
	}
	private function checkUrl()
	{
		if($this->config['domain'] != $_SERVER['HTTP_HOST'])
		{
			parent::_redirect($_SERVER['REQUEST_URI']);
		}
		if($this->config['parking'] == true && substr($this->config['domain'], -4) == '.com')
		{
			parent::_redirect("/parking.html");
		}
	}
	
	protected function _view($data = array(), $file='')
	{
		$_PATH_ = $this->config['development'] ? "/app" : "/theme/{$this->config['theme']}";
		if( !$file)
		{
			$file = $_PATH_.$this->uri['path'].$this->uri['class'].'/'.$this->uri['method'].'.html';
		}
		else
		{
			$file = $_PATH_.$file;
		}
		$this->tpl->define('view', $file);
		$this->tpl->assign($data);
		$this->tpl->assign('config', $this->config);
		$this->tpl->assign('session', $this->session);
		$this->tpl->assign('default_js', $_PATH_.$this->uri['path'].$this->uri['class'].'/js/'.$this->uri['method'].'.js');
		$this->tpl->assign('default_css', $_PATH_.$this->uri['path'].$this->uri['class'].'/css/'.$this->uri['method'].'.css');
		$view = $this->tpl->fetch('view');
		
		$this->tpl->define('_CONTENTS_', $_PATH_.'/layout.'.$this->layout.'.html');
		$this->tpl->assign('view', $view);
		$this->tpl->assign('PATH', $this->uri['path'].$this->uri['class'].'/');
		$this->tpl->assign('KEYWORDS', $this->keywords);
		$this->tpl->assign('DESCRIPTION', $this->description);
// 		$this->_menu();
		$this->_message_count();
// 		$this->_search_rank();
		$output = $this->tpl->fetch('_CONTENTS_');
		parent::_parsing($output);
		print($output);
	}
	
	protected function _menu()
	{
		$mm = Wdk::get('MenuManager');
		$mm->loadAll();
		$menus = array();
		foreach($mm['lists'] as $menu)
		{
			if(strlen($menu['menu_code']) == 1)
			{
				$menus[$menu['menu_code']] = array(	'name'		=>	$menu['menu_name'],
													'url'		=>	$menu['menu_url'],
													'target'	=>	$menu['menu_target'],
													'child'		=>	array()
												);
			}
			else
			{
				$pcode = substr($menu['menu_code'], 0, 1);
				$code = $menu['menu_code'];
				$menus[$pcode]['child'][$code] = array(	'name'		=>	$menu['menu_name'],
														'url'		=>	$menu['menu_url'],
														'target'	=>	$menu['menu_target'],
														'child'		=>	array()
												);
			}
		}
		$this->tpl->assign('menus', $menus);
// 		$this->tpl->assign('location', $location);
	}
	
	private function _message_count()
	{
		$mm = Wdk::get('MessageManager');
		$this->tpl->assign('messages_num', $mm->NewMessageNum($this->session['user_seq']));
		$upm = Wdk::get('UserPointManager');
		$this->tpl->assign('user_points', $upm->getUserPoints($this->session['user_seq']));
		
	}
	
	private function _search_rank()
	{
		$sm = Wdk::get('SearchManager');
		$this->tpl->assign('search_rank', $sm->rank(3, 7));
	}
	
	protected function _isLogin()
	{
		return $this->session->isLogin();
	}
	protected function _loginPage()
	{
		parent::_redirect('/user/login');
	}
	
	private function _currentAccess()
	{
		if(! is_dir(CACHE_PATH.'au'))
		{
			mkdir(CACHE_PATH.'au');
			mkdir(CACHE_PATH.'au/member');
			mkdir(CACHE_PATH.'au/guest');
		}
		$time = fileatime(CACHE_PATH.'au/check');
		$clear = false;
		$now = mktime();
		$interval = 30;
		if($now > $time + $interval)
		{
			file_put_contents(CACHE_PATH.'au/check', '');
			$clear = true;
		}
		if($this->_isLogin())
		{
			file_put_contents(CACHE_PATH.'au/member/'.$this->session['id'], '');			
		}
		else
		{
			file_put_contents(CACHE_PATH.'au/guest/'.$this->session['id'], '');			
		}
		$mf = array_diff(scandir(CACHE_PATH.'au/member/', false), array(".","..",".svn"));
		$gf = array_diff(scandir(CACHE_PATH.'au/guest/', false), array(".","..",".svn"));
		if($clear == true)
		{
			foreach($mf as $file)
			{
				if($now > fileatime(CACHE_PATH.'au/member/'.$file) + $interval)	@unlink(CACHE_PATH.'au/member/'.$file);
			}
			foreach($gf as $file)
			{
				if($now > fileatime(CACHE_PATH.'au/guest/'.$file)+$interval)	@unlink(CACHE_PATH.'au/guest/'.$file);				
			}
		}
		$this->tpl->assign('au', array('member'=>count($mf),'guest'=>count($gf)));
	}
}